const ejs = require('ejs'); // 需要ejs渲染template

module.exports = async (ctx, renderer, template) => {
  // 指定响应头
  ctx.headers['Content-Type'] = 'text/html';

  const context = { url: ctx.path };

  try {
    const appString = await renderer.renderToString(context);

    const { title } = context.meta.inject();
    
    const html = ejs.render(template, {
      appString,
      styles: context.renderStyles(),
      scripts: context.renderScripts(),
      title: title.text(),
    });
    ctx.body = html;
  } catch (error) {
    console.log('render error', error);
    throw error;
  }
}